// Variables
float4x4 WorldViewProjectionMatrix;    //!< World Matrix
sampler TextureSampler : register(s0); //!< The texture

///
/// @brief Vertex Shader Input Struct
/// 
struct VertexShaderInput
{
    float4 Position  : POSITION0;
	float4 Color     : COLOR0;
	float2 TexCoords : TEXCOORD0;
};

///
/// @brief Vertex Shader Output  Struct
/// 
struct VertexShaderOutput
{
    float4 Position  : POSITION0;
	float4 Color     : COLOR0;
	float2 TexCoords : TEXCOORD0;
};

///
/// @brief Simple bypassing vertex shader.
///
VertexShaderOutput DelinearizeVertexShader(VertexShaderInput input)
{
    VertexShaderOutput output;

    // Set the position
    output.Position  = mul(input.Position, WorldViewProjectionMatrix);
	output.Color     = input.Color;
	output.TexCoords = input.TexCoords;
	
    // Return the value.
    return output;
}

///
/// @brief Linearizing Pixel Shader
///
float4 DelinearizePixelShader(VertexShaderOutput input) : COLOR0
{
	// Calculate the offset.
	/* Option 1
	float2 Offset = float2(input.TexCoords.x * 2 - 1, input.TexCoords.y * 2 - 1);
	float2 NewCoords = float2(input.TexCoords.x, (Offset.y * abs(Offset.x) + 1) / 2);
	*/
	
	/* Option 2 */
	float param;
	if (input.TexCoords.x > 0.5)
	{
		param = 1 - (input.TexCoords.x + input.TexCoords.y - 2 * input.TexCoords.x * input.TexCoords.y);
	}
	else
	{
		param = input.TexCoords.x + input.TexCoords.y - 2 * input.TexCoords.x * input.TexCoords.y;
	}
	float2 NewCoords = float2(input.TexCoords.x, param);
	// Return the color mask.
	return float4( tex2D(TextureSampler, NewCoords).a, tex2D(TextureSampler, NewCoords.yx).a, 0, 1);
}

technique Delinearize
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 DelinearizeVertexShader();
        PixelShader  = compile ps_2_0 DelinearizePixelShader();
    }
}
